<script lang="ts">
import { ref, defineComponent, toRaw } from "vue";
import { showToast } from 'vant';
import 'vant/es/toast/style'
import { cityNumStore } from '@/stores/city'
import router from "@/router";
import { onMounted } from "vue";
import { essayStore, essayncStore } from '@/stores/publish';
import { useRoute } from "vue-router";
export default defineComponent({
    setup() {
        let route = useRoute();
        let sentNoteTitle = ref();

        let id = ref(null);

        // 随机数
        let sj = () => {
            return Math.random().toString(16).slice(2, 20);
        }


        let keep = () => {
            // console.log("keep");
            let articlecg = {
                cityNum: cityNumStore().$state.cityNum,
                pics: fileList.value || ['https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.jpg'],
                title: sentNoteTitle.value,
                content: noteContent.value.value,
                id: sj()
            }
            console.log(articlecg.pics);
            article.push(articlecg);
            showToast('已保存到草稿箱')
            router.back()

        }
        let cancel = () => {
            router.back();
        }
        let show = ref(false);
        let noteContent = ref()
        const fileList = ref([]);
        let article: any = toRaw(essayStore().$state.essay);


        let show2 = ref(false)
        const onClickLeft = () => {
            if (route.query.set == undefined) {
                if (fileList.value == undefined && sentNoteTitle.value == undefined && noteContent.value.value == '') {
                    router.back();
                } else {
                    show2.value = true
                }
            }else {
                let draft:any = toRaw(essayStore().$state.essay)
                console.log(draft);
                let index = draft.findIndex((item: any) => {
                    return item.id == String(id.value);
                })
                let articlecg = {
                    cityNum: cityNumStore().$state.cityNum,
                    pics: fileList.value,
                    title: sentNoteTitle.value,
                    content: noteContent.value.value,
                    id: id.value
                }
                draft.splice(index , 1 , articlecg)
                
                router.back();
            }
        }
        // JSON.parse(window.localStorage.getItem('token')
        let commentList: any = toRaw(essayncStore().$state.essaync)
        let sendArticle = () => {
            if (fileList.value == undefined || sentNoteTitle.value == undefined || noteContent.value.value.length == 0) {
                showToast('图片，标题和内容三个不有空位~')
                return
            } else {
                let obj = JSON.parse(localStorage.getItem('user') || '{}');

                let articlecg = {
                    cityNum: cityNumStore().$state.cityNum,
                    pics: fileList.value,
                    title: sentNoteTitle.value,
                    content: noteContent.value.value,
                    id: sj()
                }
                commentList.unshift(articlecg)
                console.log(commentList);
                obj.article.push(articlecg);
                window.localStorage.setItem('user' , JSON.stringify(obj));

                showToast('发表成功')
                router.back();
            }

        }
        onMounted(() => {
            article = toRaw(essayStore().$state.essay);
            console.log(article);
            fileList.value = article.pics
            sentNoteTitle.value = article.title
            noteContent.value.value = article.content || ''
            if (route.query.set !== undefined) {
                let obj = JSON.parse(route.query.set as string)
                console.log(obj);
                noteContent.value.value = obj.content;
                sentNoteTitle.value = obj.title;
                fileList.value = obj.pics;
                id.value = obj.id
            }
        })
        return {
            show,
            fileList,
            sentNoteTitle,
            onClickLeft,
            sendArticle,
            noteContent,
            show2,
            keep,
            cancel,
        }
    }
})
</script>
<template>
    <div class="ReleaseView">
        <div class="ReleaseView-Header">
            <div class="ReleaseView-operate">
                <div class="operate-left">
                    <div class="operate-cross" @click="onClickLeft">
                        <van-icon name="cross" />
                    </div>
                    <div class="operate-tips" @click="show = !show">
                        <van-icon name="like" color="red" />
                        <span> 如何发布优质内容？</span>
                    </div>
                </div>
                <van-button type="success" color="#12c1b6" @click="sendArticle">发布</van-button>
            </div>
        </div>
        <div class="send-note">
            <div class="sent-note-img">
                <van-uploader v-model="fileList" reupload max-count="1" />
            </div>
            <div class="sent-note-title">
                <input type="text" placeholder="请输入标题" v-model="sentNoteTitle">
            </div>

            <div class="sent-note-content">
                <textarea cols="30" rows="15" placeholder="请输入正文" id="noteContent" ref="noteContent"></textarea>
            </div>

        </div>
        <van-dialog v-model:show="show" title="动态发布提示" messageAlign="left"
            :message="`这样分享更容易被推荐:\n1.与家居家装相关。\n2.有标题,并在标题中提炼内容主旨。\n3.图文兼备，图文相符。\n4.逻辑清晰，分段易读。\n含以下信息的内容将不会被推荐:\n1.其他网址链接、联系方式、二维码、团购等文字或图片`"
            confirmButtonText="我知道了">
        </van-dialog>
        <van-dialog v-model:show="show2" title="保留此次编辑?" show-cancel-button confirmButtonText="保留" cancel-button-text="不保留"
            @confirm="keep" @cancel="cancel">
        </van-dialog>
    </div>
</template>
<style lang="scss">
.ReleaseView {
    width: 100%;
    height: 100vh;
    background-color: #ffffff;
    padding: 12px 12px 0;
    box-sizing: border-box;

    .ReleaseView-Header {
        margin-bottom: 20px;

        .ReleaseView-operate {
            display: flex;
            align-items: center;

            .operate-left {
                display: flex;
                flex: 1;
                justify-content: space-between;
                align-items: center;

                .operate-cross {
                    font-size: 20px;
                }

                .operate-tips {
                    font-size: 12px;
                }
            }

            .van-button {
                height: 30px;
                border-radius: 18px;
                transform: scale(0.9);
            }
        }
    }

    .sent-note-img {
        width: 100%;
        height: 80px;

        .van-uploader__upload {
            width: 80px;
            height: 80px;
            // border-radius: 10px;
            border: 2px solid #f2f2f2;
        }

        .van-uploader__preview {
            width: 80px;
            height: 80px;
            margin-right: 7px;

            // border-radius: 10px;
            &:nth-of-type(4),
            &:nth-of-type(8) {
                margin-right: 0px;
            }

            .van-uploader__preview-image {
                width: 80px;
                height: 80px;

                // border-radius: 10px;
                img {
                    width: 80px;
                    height: 80px;
                    // border-radius: 10px;
                }
            }

        }

    }

    .sent-note-title {
        margin-top: 13px;

        input {
            outline: none;
            border: none;
            height: 46px;
            width: 100%;
            border-bottom: 2px solid #f2f2f2
        }
    }

    .sent-note-content {
        textarea {
            width: 100%;
            resize: none;
            outline: none;
            border: none;
            padding-top: 20px;
            font-size: 14px;
        }
    }

    .van-popup {
        .van-button {
            .van-button__content {
                span {
                    color: #12c1b6;
                }
            }
        }
    }
}
</style>